package com.wang.myblog.service.impl;

import com.wang.myblog.controller.common.UploadController;
import com.wang.myblog.entity.Admin;
import com.wang.myblog.mapper.AdminMapper;
import com.wang.myblog.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin queryById(Integer id) {
        return adminMapper.queryById(id) ;
    }

    @Override
    public boolean update(Admin admin) {
        Admin admin1=adminMapper.queryById(1);
        if(adminMapper.update(admin)==1) {
            if(!admin1.getImg().equals(admin.getImg())) {
                UploadController.deleteCommonImg(admin1.getImg());
            }
            if (!admin1.getWechat().equals(admin.getWechat())){
                UploadController.deleteCommonImg(admin1.getWechat());
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean login(String name, String pwd) {
        if(adminMapper.queryByNameAndPwd(name,pwd)!=null){
            Admin admin=adminMapper.queryById(1);
            admin.setRecentLogin(new Date());
            adminMapper.update(admin);
            return true;
        }
        return false;
    }
}
